package com.soccertgc.core
{
	
	import com.soccertgc.flex.data.FlexDeck;
	import com.soccertgc.logging.Dumper;

	
	public class User extends Player
	{
		
		// -------------------------------------------------------------------------------
		// CONSTANTS
		// -------------------------------------------------------------------------------	
		
		// Property names
		protected static const 	PROPERTY_USER_ID:String				= "userID";
		protected static const 	PROPERTY_SESSION_TOKEN:String		= "sessionToken";
		protected static const 	PROPERTY_DECK_SIZE:String			= "deckSize";
		
		// -------------------------------------------------------------------------------
		// FIELDS
		// -------------------------------------------------------------------------------
		
		private var _id:uint;
		private var _deckSize:int;			// LT_TODO: eliminare e infilare dentro GameOptions
		private var _sessionToken:String;
		
		// -------------------------------------------------------------------------------
		// CONSTRUCTOR
		// -------------------------------------------------------------------------------
		
		public function User(data:Object = null)
		{
			// Clear
			clear();
			// If data is provided, initializes the instance
			if (data != null) load(data);
		}
		
		// -------------------------------------------------------------------------------
		// PROPERTIES
		// -------------------------------------------------------------------------------
		
		[Bindable]
		public function get id():uint
		{
			return _id;
		}
		
		public function set id(value:uint):void
		{
			_id = value;
		}
		
		public function get sessionToken():String
		{
			return _sessionToken;
		}
		
		public function get deckSize():int
		{
			return _deckSize;
		}
		
		public function get isLoggedIn():Boolean
		{
			// return loaded;
			return _id != DataObject.INVALID_ID;
		}

		// -------------------------------------------------------------------------------
		// PRIVATE METHODS
		// -------------------------------------------------------------------------------
		
		// DataObject implementation
		
		override protected function validate(data:Object):void
		{
			// Ancestor call
			super.validate(data);
			// Required fields
			if (!data.hasOwnProperty(PROPERTY_USER_ID)) 	throwLoadMissingPropError(PROPERTY_USER_ID);
			if (!data.hasOwnProperty(JSON_PROPERTY_FIRST_NAME))	throwLoadMissingPropError(JSON_PROPERTY_FIRST_NAME);
			if (!data.hasOwnProperty(JSON_PROPERTY_LAST_NAME))	throwLoadMissingPropError(JSON_PROPERTY_LAST_NAME);
			if (!data.hasOwnProperty(JSON_PROPERTY_EMAIL))		throwLoadMissingPropError(JSON_PROPERTY_EMAIL);
		}
		
		override protected function fill(data:Object):void
		{
			// Ancestor
			super.fill(data);
			// Required fields
			_id = data[User.PROPERTY_USER_ID];
			// Optional fields
			if (data.hasOwnProperty(User.PROPERTY_SESSION_TOKEN))	_sessionToken = data[User.PROPERTY_SESSION_TOKEN];
			if (data.hasOwnProperty(User.PROPERTY_DECK_SIZE)) 		_deckSize 	  = data[User.PROPERTY_DECK_SIZE];
		}


		// -------------------------------------------------------------------------------
		// PUBLIC METHODS
		// -------------------------------------------------------------------------------

		override public function equals(player:Player):Boolean
		{
			if (player is User && super.equals(player))
			{
				// User cast
				var user:User = player as User;
				// Check User properties
				return 	_id				== user.id			&&
						_deckSize		== user.deckSize	&&
						_sessionToken	== user.sessionToken;
			}
			return false;
		}
		
		override public function copy(player:Player):void
		{	
			// Ancestor copy
			super.copy(player);
			// User cast
			var user:User = player as User;
			if (user != null)
			{
				_id				= user.id;
				_deckSize		= user.deckSize;
				_sessionToken	= user.sessionToken;
			}
		}
		
		override public function clone():Player
		{
			var user:User = new User();
			user.copy(this);
			return user;
		}
		
		override public function clear():void
		{
			// Ancestor
			super.clear();
			// Clear data
			_id 			= DataObject.INVALID_ID;
			_sessionToken	= "";
			_deckSize		= FlexDeck.REQUIRED_ACTION_CARDS;
		}
		
		public function toString():String
		{
			return _username + "(id: " + _id + ")";
		}
		
		override public function dump(source:Array = null, indentDepth:int = 0):Array
		{
			return Dumper.dump(this, null, source, indentDepth);
		}
		
	}
}